package com.qifan.energy.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.qifan.energy.base.BaseActivity;
import com.qifan.energy.R;
import com.qifan.energy.bean.result.MemberInfo;
import com.qifan.energy.global.Keys;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.ActivityStackUtil;
import com.qifan.energy.util.SharedPreferencesUtil;
import com.qifan.energy.util.ToastUtils;
import com.qifan.energy.widget.CustomTitleBar;
import com.qifan.energy.widget.dialog.LoadingUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class EditAlipayActivity extends BaseActivity {

    @BindView(R.id.title_layout_bar)
    CustomTitleBar titleBar;
    @BindView(R.id.realname_et)
    EditText realnameEt;
    @BindView(R.id.alipay_acount_et)
    EditText alipayAcountEt;

    /*@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
    }*/

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_alipay;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        /* 更改状态栏 */
        changeStatusTransparentFontBlack();

        int certifyType = getIntent().getIntExtra(Keys.BINDING_ALIPAY_TYPE,0);
        if (certifyType == 1){
            titleBar.setTitleBarTitle("修改支付宝");
            MemberInfo memberInfo = SharedPreferencesUtil.getData(Keys.USER_INFO_MEMBER_KEY,new MemberInfo());
            String realName = memberInfo.getRealName();
            String alipayAccount = memberInfo.getAlipayAccount();
            realnameEt.setText(realName);
            realnameEt.setSelection(realnameEt.getText().length());
            alipayAcountEt.setText(alipayAccount);
        }
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.done_btn)
    void onDoneBtnClicked(TextView doneBtn){
        String realName = realnameEt.getText().toString();
        String aliAcount = alipayAcountEt.getText().toString();
        if (TextUtils.isEmpty(realName)){
            ToastUtils.showShort("真实姓名必须填写哦~");
            return;
        }
        if (TextUtils.isEmpty(aliAcount)){
            ToastUtils.showShort("支付宝账号必须填写哦~");
            return;
        }

        //更改按钮状态
        doneBtn.setEnabled(false);
        LoadingUtils.show(this);
        NetUtils.memberBalanceAliPay(aliAcount, realName, new MyObserver<MemberInfo>(this) {
            @Override
            public void onSuccess(MemberInfo result) {
                //更新用户信息保存本地
                SharedPreferencesUtil.putData(Keys.USER_INFO_MEMBER_KEY,result);

                doneBtn.setEnabled(true);
                LoadingUtils.hide();

                //提示并跳转
                ToastUtils.showShort("绑定完成!");
                setResult(RESULT_OK);
                ActivityStackUtil.finishTopActivity();
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                doneBtn.setEnabled(true);
                LoadingUtils.hide();
                ToastUtils.showShort(errorMsg);
            }
        });
    }
}
